package com.panshi.qa.provider.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panshi.qa.api.vo.QaQuestionFrontdesk;
import com.panshi.qa.provider.modules.domain.UserCollect;
import com.panshi.qa.provider.modules.mapper.UserCollectMapper;
import com.panshi.qa.provider.modules.service.QaQuestionService;
import com.panshi.qa.provider.modules.service.UserCollectService;
import com.panshi.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserCollectServiceImpl extends ServiceImpl<UserCollectMapper, UserCollect> implements UserCollectService {

    @Autowired
    QaQuestionService qaQuestionService;

    @Override
    //增加问题到指定收藏表
    public R insertCollect(QaQuestionFrontdesk qaQuestionFrontdesk) {
        System.out.println("收藏问题对象           "+qaQuestionFrontdesk);
        List<UserCollect> userCollectList = this.baseMapper.selectList(new QueryWrapper<UserCollect>().eq("user_Id",qaQuestionFrontdesk.getUserId())
                .eq("t_id",qaQuestionFrontdesk.getId()));

        if(userCollectList.size()>0){
            return R.error().msg("你已收藏过该问题");
        }

        //添加到收藏表
        int count = this.baseMapper.insertCollect(qaQuestionFrontdesk);
        //修改问题表的收藏量
        this.baseMapper.updateQuestion(qaQuestionFrontdesk);
        //修改搜索表的收藏量
        this.baseMapper.updateThSearch(qaQuestionFrontdesk);

        if(count>0){
            //查询指定问题对象
            com.panshi.qa.provider.modules.domain.QaQuestion qaQuestion = qaQuestionService.selectById(qaQuestionFrontdesk.getId());
            return R.ok().msg("问题信息添加到个人收藏表成功···").data("data",qaQuestion.getCollectNum());
        }
        return R.error().msg("问题信息添加到个人收藏表失败···");
    }
}
